using System;
using System.Collections.Generic;
using System.Text;
using Fieldbook.Entities;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Fieldbook.Common;
using System.Data.Common;

namespace Fieldbook.DAL
{
    public static class ResourceCategoryDAO
    {
        #region insert
        private static DbCommand GetInsertCommand(SqlDatabase db)
        {
            string insertSQL = "INSERT INTO dbo.ResourceCategory (DESCRIPTION, HOUSEHOLDID) VALUES (@DESCRIPTION, @HOUSEHOLDID) SET @CATEGORYID = SCOPE_IDENTITY()";
            DbCommand insertCommand = db.GetSqlStringCommand(insertSQL);
            db.AddOutParameter(insertCommand, "@CATEGORYID", DbType.Int64, 8);
            db.AddInParameter(insertCommand, "@DESCRIPTION", DbType.String);
            db.AddInParameter(insertCommand, "@HOUSEHOLDID", DbType.Int64);
            return insertCommand;
        }
        private static void SetInsertValues(SqlDatabase db, DbCommand cmd, ResourceCategory entity)
        {
            db.SetParameterValue(cmd, "@DESCRIPTION", entity.Description);
            db.SetParameterValue(cmd, "@HOUSEHOLDID", entity.HouseHoldID);
        }
        public static bool InsertResourceCategory(ResourceCategory entity)
        {
            bool res = false;
            SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
            DbCommand cmd = GetInsertCommand(db);
            SetInsertValues(db, cmd, entity);
            res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
            if (res)
            {
                entity.CategoryID = (long?)db.GetParameterValue(cmd, "@CATEGORYID");
            }
            return res;
        }
        #endregion

        #region delete
        private static DbCommand GetDeleteCommand(SqlDatabase db)
        {
            string deleteSQL = "DELETE FROM dbo.ResourceCategory WHERE CATEGORYID = @CATEGORYID";
            DbCommand deleteCommand = db.GetSqlStringCommand(deleteSQL);
            db.AddInParameter(deleteCommand, "@CATEGORYID", DbType.Int64);
            return deleteCommand;
        }

        private static void SetDeleteValues(SqlDatabase db, DbCommand cmd, long categoryID)
        {
            db.SetParameterValue(cmd, "@CATEGORYID", categoryID);
        }

        public static bool DeleteResourceCategory(long categoryID)
        {
            bool res = false;
            SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
            DbCommand cmd = GetDeleteCommand(db);
            SetDeleteValues(db, cmd, categoryID);
            res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
            return res;
        }

        public static bool DeleteResourceCategory(ResourceCategory entity)
        {
            return DeleteResourceCategory((long)entity.CategoryID);
        }
        #endregion

        #region select
        private static List<ResourceCategory> LoadResourceCategories(SqlDatabase db, DbCommand command)
        {
            List<ResourceCategory> res = new List<ResourceCategory>();
            IDataReader reader = null;
            try
            {
                reader = db.ExecuteReader(command);
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        ResourceCategory entity = new ResourceCategory();
                        entity.CategoryID = reader.GetInt64(0);
                        entity.Description = reader.IsDBNull(1) ? null : reader.GetString(1);
                        entity.HouseHoldID = reader.IsDBNull(2) ? null : (long?)reader.GetInt64(2);
                        res.Add(entity);
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                if (reader != null && !reader.IsClosed)
                {
                    reader.Close();
                }
                res = null;
                throw ex;
            }
            return res;
        }

        public static List<ResourceCategory> LoadResourceCategories(long userID)
        {
            string selectSQL = @"SELECT RC.CATEGORYID, RC.DESCRIPTION, RC.HOUSEHOLDID
FROM RESOURCECATEGORY RC 
LEFT OUTER JOIN HOUSEHOLD HH ON RC.HOUSEHOLDID = HH.HEADID OR RC.HOUSEHOLDID IS NULL
WHERE HH.MEMBERID = @MEMBERID";

            SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
            DbCommand cmd = db.GetSqlStringCommand(selectSQL);
            db.AddInParameter(cmd, "@MEMBERID", DbType.Int64, userID);
            return LoadResourceCategories(db, cmd);
        }
        #endregion
    }
}
